package com.hoshiicloud.saleorder.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.QueryException;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.saleorder.entity.ActivitiesList;
import com.hoshiicloud.saleorder.param.ActivitiesListAddParam;
import com.hoshiicloud.saleorder.param.ActivitiesListQueryParam;
import com.hoshiicloud.saleorder.param.ActivitiesListUpdateParam;
import com.hoshiicloud.saleorder.service.ActivitiesListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 活动流水 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-03
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/saleorder/activities-list")
@Api(value = "活动流水", tags = {"活动流水"})
public class ActivitiesListController {

    @Autowired
    private ActivitiesListService activitiesListService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询活动流水
     */
    @ApiOperation(value = "分页查询活动流水")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid ActivitiesListQueryParam param) {
        log.info("分页查询活动流水");
        try {
            QueryWrapper<ActivitiesList> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(param.getSubId() != null, ActivitiesList::getSubId, param.getSubId())
                    .eq(StringUtils.isNotBlank(param.getActivityId()), ActivitiesList::getActivityId, param.getActivityId())
                    .eq(param.getReward() != null, ActivitiesList::getReward, param.getReward())
                    .eq(param.getQuantity() != null, ActivitiesList::getQuantity, param.getQuantity())
                    .eq(param.getSpuId() != null, ActivitiesList::getSpuId, param.getSpuId())
                    .eq(param.getSkuId() != null, ActivitiesList::getSkuId, param.getSkuId())
                    .eq(param.getStatus() != null, ActivitiesList::getStatus, param.getStatus())
                    .eq(StringUtils.isNotBlank(param.getRefNo()), ActivitiesList::getRefNo, param.getRefNo())
                    .eq(true, ActivitiesList::getDeleted, param.getDeleted() == null ? 0 : param.getDeleted());
            IPage<ActivitiesList> page = new Page<>(Long.parseLong(param.getPageNumber()),
                    Long.parseLong(param.getPageSize()));
            String key = "ActivitiesListQueryParam{" + param.toString() + "} ,Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param
                    .getPageSize() + "}";
            return Response.data(activitiesListService.page(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询活动流水异常 {}", e.getMessage());
            throw new QueryException("分页查询活动流水异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询活动流水
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询活动流水")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(activitiesListService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增活动流水")
    public Response save(@RequestBody @Valid ActivitiesListAddParam param) {
        ActivitiesList entity;

        try {
            entity = BeanUtils.covertType(param, ActivitiesList.class);
        } catch (Exception e) {
            log.error("新增活动流水类型转换异常{}", e.getMessage());
            throw new ServiceException("新增活动流水异常");
        }
        return Response.data(activitiesListService.save(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改活动流水
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改活动流水")
    public Response update(@RequestBody @Valid ActivitiesListUpdateParam param) {
        ActivitiesList entity;
        try {
            entity = BeanUtils.covertType(param, ActivitiesList.class);
        } catch (Exception e) {
            log.error("类型转换异常{}", e.getMessage());
            throw new ServiceException("修改活动流水异常");
        }

        return Response.data(activitiesListService.updateById(entity));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除活动流水
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除活动流水")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        // return Response.success("删除活动流水成功");
        return Response.data(activitiesListService.deleteById(id));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询活动流水")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            ids.sort(null);
            return Response.data(activitiesListService.getByIds(ids, StringUtils.join(ids, "")));
        } else {
            return Response.data(null);
        }
    }

}
